package vsvteam.outsource.leanappandroid.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class TStepsDataBaseHandler extends SQLiteOpenHelper {
	// All Static variables
	// Database Version
	private static final int DATABASE_VERSION = 1;

	// Database Name
	private static final String DATABASE_NAME = "ProjectManager";

	// Contacts table name
	private static final String TABLE_T_STEPS = "TABLE_T_STEPS";
	// Contacts Table Columns names
	private static final String KEY_PROJECT_ID = "KEY_PROJECT_ID";
	private static final String KEY_PROCESS_NAME = "KEY_PROCESS_NAME";
	private static final String KEY_STEP_ID = "KEY_STEP_ID";
	private static final String KEY_STEP_NO = "KEY_STEP_NO";
	private static final String KEY_STEP_DESCRIPTION = "KEY_STEP_DESCRIPTION";
	private static final String KEY_VERSION_ID = "KEY_VERSION_ID";
	private static final String KEY_PREVIOUS_VERS_ID = "KEY_PREVIOUS_VERS_ID";
	private static final String KEY_VIDEO_FILE_NAME = "KEY_VIDEO_FILE_NAME";

	public TStepsDataBaseHandler(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	// create database
	@Override
	public void onCreate(SQLiteDatabase db) {
		String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_T_STEPS + "(" + KEY_PROJECT_ID
				+ " INTEGER PRIMARY KEY," + KEY_PROCESS_NAME + " TEXT," + KEY_STEP_ID + " INTEGER,"
				+ KEY_STEP_NO + " INTEGER," + KEY_STEP_DESCRIPTION + " TEXT," + KEY_VERSION_ID
				+ " INTEGER," + KEY_PREVIOUS_VERS_ID + " TEXT," + KEY_VIDEO_FILE_NAME + " TEXT"
				+ ")";
		db.execSQL(CREATE_CONTACTS_TABLE);
	}

	// update database
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// Drop older table if existed
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_T_STEPS);

		// Create tables again
		onCreate(db);
	}

	/**
	 * All CRUD(Create, Read, Update, Delete) Operations
	 */

	// Adding new step
	public void addNewStep(TStepsDataBase tSteps) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_PROJECT_ID, tSteps.getProjectID()); // project id
		values.put(KEY_PROCESS_NAME, tSteps.getProcessName()); // process name
		values.put(KEY_STEP_ID, tSteps.getStepID()); // step id
		values.put(KEY_STEP_NO, tSteps.getStepNo());// step no
		// step description
		values.put(KEY_STEP_DESCRIPTION, tSteps.getStepDescription());
		values.put(KEY_VERSION_ID, tSteps.getVersionID());// version id
		// previous version id
		values.put(KEY_PREVIOUS_VERS_ID, tSteps.getPreviousVesID());
		// video file name
		values.put(KEY_VIDEO_FILE_NAME, tSteps.getVideoFileName());
		// Inserting Row
		db.insert(TABLE_T_STEPS, null, values);
		// close db after use
		db.close(); // Closing database connection
	}

	// Getting single step
	public TStepsDataBase getStep(int id) {
		SQLiteDatabase db = this.getReadableDatabase();

		Cursor cursor = db.query(TABLE_T_STEPS, new String[] { KEY_PROJECT_ID, KEY_PROCESS_NAME,
				KEY_STEP_ID, KEY_STEP_NO, KEY_STEP_DESCRIPTION, KEY_VERSION_ID,
				KEY_PREVIOUS_VERS_ID, KEY_VIDEO_FILE_NAME }, KEY_PROJECT_ID + "=?",
				new String[] { String.valueOf(id) }, null, null, null, null);

		if (cursor != null)
			cursor.moveToFirst();

		TStepsDataBase tStep = new TStepsDataBase(Integer.parseInt(cursor.getString(0)),
				cursor.getString(1), Integer.parseInt(cursor.getString(2)), Integer.parseInt(cursor
						.getString(3)), cursor.getString(4), Integer.parseInt(cursor.getString(5)),
				Integer.parseInt(cursor.getString(6)),cursor.getString(7));

		db.close();
		// return contact
		return tStep;
	}

}
